package com.wyh.controller.g3controller;

import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.result.Result;
import com.wyh.domain.po.g3po.Feeclassification;
import com.wyh.service.g3service.FeeclassificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "费项分类")
@RestController
@RequestMapping("/feeclassification")
public class FeeclassificationController {

    @Autowired
    private FeeclassificationService feeclassificationService;

    @ApiOperation("查询所有分类")
    @GetMapping("/tree")
    public Result<List<Map<String, Object>>> getTreeStructure() {
        List<Map<String, Object>> treeStructure = feeclassificationService.getTreeStructure();
        return Result.success(treeStructure);
    }

    @ApiOperation("添加分类")
    @PostMapping("/add")
    public Result<Void> addFeeclassification(@RequestBody Feeclassification feeclassification) {
        try {
            feeclassificationService.addFeeclassification(feeclassification);
            return Result.success();
        } catch (IllegalArgumentException e) {
            return Result.error(ResultCodeEnum.SYSTEM_ERROR.code, e.getMessage());
        }
    }

    @ApiOperation("删除分类")
    @DeleteMapping("/delete/{id}")
    public Result<Void> deleteFeeclassification(@PathVariable Integer id) {
        try {
            feeclassificationService.deleteFeeclassification(id);
            return Result.success();
        } catch (IllegalArgumentException e) {
            return Result.error(ResultCodeEnum.SYSTEM_ERROR.code, e.getMessage());
        }
    }

    @ApiOperation("编辑分类")
    @PutMapping("/edit/{id}")
    public Result<Void> editFeeclassification(@PathVariable Integer id, @RequestBody Feeclassification feeclassification) {
        try {
            feeclassificationService.editFeeclassification(id, feeclassification);
            return Result.success();
        } catch (IllegalArgumentException e) {
            return Result.error(ResultCodeEnum.SYSTEM_ERROR.code, e.getMessage());
        }
    }
    @ApiOperation("搜索分类")
    @GetMapping("/search")
    public Result<List<Feeclassification>> searchFeeclassification(@RequestParam String keyword) {
        List<Feeclassification> searchResults = feeclassificationService.searchFeeclassification(keyword);
        return Result.success(searchResults);
    }
}
